package com.example.bookstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bookstore.common.BaseContext;
import com.example.bookstore.common.PageResult;
import com.example.bookstore.common.Result;
import com.example.bookstore.dto.QueryCartParamsDto;
import com.example.bookstore.entity.Book;
import com.example.bookstore.entity.Collect;
import com.example.bookstore.mapper.BookMapper;
import com.example.bookstore.mapper.CollectMapper;
import com.example.bookstore.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    BookMapper bookMapper;
    @Override
    public Result deleteCollect(Integer id,int userId) {
        LambdaQueryWrapper<Collect> qw=new LambdaQueryWrapper<>();
        qw.eq(Collect::getBookId,id);
        qw.eq(Collect::getUserId,userId);
        collectMapper.delete(qw);
        return Result.success("已取消收藏");
    }

    @Override
    public Result<String> isCollected(Integer id,int userId) {
        LambdaQueryWrapper<Collect> qw=new LambdaQueryWrapper<>();
        qw.eq(Collect::getBookId,id);
        qw.eq(Collect::getUserId,userId);
        Collect collect = collectMapper.selectOne(qw);
        if (collect==null){
            return null;
        }
        return Result.success("已在收藏夹");
    }

    @Override
    public PageResult<Collect> selectcollect(int pageSize, int currentPage, QueryCartParamsDto params) {
        Page<Collect> page=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Collect> qw=new LambdaQueryWrapper<>();
        long userId = BaseContext.getCurrentId();
        qw.eq(Collect::getUserId,userId);
        if (!StringUtils.isEmpty(params.getBookName())){
            List<Integer> ids=bookMapper.selectLikBookName("%"+params.getBookName()+"%");
            if (ids.size()==0){
                qw.in(Collect::getBookId,-1);//模糊查询不到数据
            }else {
                qw.in(Collect::getBookId,ids);
            }
        }
        qw.ge(!StringUtils.isEmpty(params.getStartDate()),Collect::getAddTime,params.getStartDate());
        qw.le(!StringUtils.isEmpty(params.getEndDate()),Collect::getAddTime,params.getEndDate());
        qw.orderByDesc(Collect::getAddTime);
        Page<Collect> pageResult = collectMapper.selectPage(page, qw);
        List<Collect> records = pageResult.getRecords();
        records.forEach(item->{
            Book book = bookMapper.selectById(item.getBookId());
            item.setBookImg(book.getBookImg());
            item.setBookName(book.getBookName());
            item.setBookPrice(book.getPrice());
            item.setInventory(book.getInventory());
            item.setStatus(book.getStatus());
        });
        long total = pageResult.getTotal();
        PageResult<Collect> result=new PageResult<>(records,total,currentPage,pageSize);
        return result;
    }
}
